///////////////////////////////////////////////////////////
//  Station.cpp
//  Implementation of the Class Station
//  Created on:      21-sep-2007 15:40:06
///////////////////////////////////////////////////////////

#include "Station.h"
#include <iostream>

#define SIZE_CODE 5
#define SIZE_NUMBER 8
#define SIZE_ELEVATION 10
#define SIZE_LATITUDE 10
#define SIZE_LONGITUDE 13

#define SIZE_CODE_SCHEMA 4
#define SIZE_NUMBER_SCHEMA 6
#define SIZE_ELEVATION_SCHEMA 9
#define SIZE_LATITUDE_SCHEMA 8
#define SIZE_LONGITUDE_SCHEMA 9

#ifndef UNO
	#define UNO 1
#endif

#ifndef ZERO
	#define ZERO 0
#endif

campo Station::CODE[2] = {{"code",CHAR, SIZE_CODE},{0,0,0}};
campo Station::NUMBER[2] = {{"number",CHAR, SIZE_NUMBER},{0,0,0}};
campo Station::ELEVATION[2] = {{"elevation",CHAR, SIZE_ELEVATION},{0,0,0}};
campo Station::LATITUDE[2] = {{"latitude",CHAR, SIZE_LATITUDE},{0,0,0}};
campo Station::LONGITUDE[2] = {{"longitude",CHAR, SIZE_LONGITUDE},{0,0,0}};

campo Station::SCHEMA[SIZE_STATION_SCHEMA] = {{"code",CHAR, SIZE_CODE}, {"number",CHAR, SIZE_NUMBER}, {"elevation",CHAR, SIZE_ELEVATION}, {"latitude",CHAR, SIZE_LATITUDE}, {"longitude",CHAR, SIZE_LONGITUDE},{0,0,0}};
	
Station::Station(){

}


Station::~Station(){

}


std::string Station::getValor(std::string atribName){

	if (Station::CODE[0].nombre == atribName){
		return this->code;
	}
	if (Station::NUMBER[0].nombre == atribName){
		return this->number;
	}
	if (Station::ELEVATION[0].nombre == atribName){
		return this->elevation;
	}
	if (Station::LATITUDE[0].nombre == atribName){
		return this->latitude;
	}
	if (Station::LONGITUDE[0].nombre == atribName){
		return this->longitude;
	}
	return "";
}


void Station::loadAtribFromNode(XMLNode* node){

	listaXML::const_iterator j = node->hijos.begin();
	std::string nombreNodo;
	for(;j != node->hijos.end() ;j++)
	{
		nombreNodo=(*j)->getNombre();
		if (nombreNodo == "code")
		{
			this->code = (*j)->getContenido();
		}
		else
		{
			if (nombreNodo == "number")
			{
				this->number = (*j)->getContenido();
			}
			else
			{
				if (nombreNodo == "elevation")
				{
					this->elevation = (*j)->getContenido();
				}
				else
				{
					if (nombreNodo == "latitude")
					{
						this->latitude = (*j)->getContenido();
					}
					else
					{
						if (nombreNodo == "longitude")
						{
							this->longitude = (*j)->getContenido();
						}
					}
				}
			}
		}
	}
}

int Station::getSchemaSize(){
	return SIZE_STATION_SCHEMA;
}
campo* Station::getSchema()
{
	return Station::SCHEMA;
}

campo* Station::getPrimaryKey()
{
	return Station::CODE;
}

void Station::loadRegistro(void* registro)
{
	campo* esquema = NULL;

	char ptrCode[SIZE_CODE + UNO];
	char ptrNumber[SIZE_NUMBER + UNO];
	char ptrElevation[SIZE_ELEVATION + UNO];
	char ptrLatitude[SIZE_LATITUDE + UNO];
	char ptrLongitude[SIZE_LONGITUDE + UNO];

	memset(ptrCode, ZERO, SIZE_CODE  + UNO);
	memset(ptrNumber, ZERO, SIZE_NUMBER  + UNO);
	memset(ptrElevation, ZERO, SIZE_ELEVATION  + UNO);
	memset(ptrLatitude, ZERO, SIZE_LATITUDE  + UNO);
	memset(ptrLongitude, ZERO, SIZE_LONGITUDE  + UNO);

	esquema = this->getSchema();
	
	RecordHandler::REG_GET(registro, esquema, "code, number, elevation, latitude, longitude", ptrCode, ptrNumber, ptrElevation, ptrLatitude, ptrLongitude);

	this->code.clear();
	this->code += ptrCode;

	this->number.clear();
	this->number += ptrNumber;

	this->elevation.clear();
	this->elevation += ptrElevation;

	this->latitude.clear();
	this->latitude += ptrLatitude;
		
	this->longitude.clear();
	this->longitude += ptrLongitude;

}


void Station::saveInRegistro(void* reg)
{
	campo* esquema = NULL;

	char ptrCode[SIZE_CODE + UNO];
	char ptrNumber[SIZE_NUMBER + UNO];
	char ptrElevation[SIZE_ELEVATION + UNO];
	char ptrLatitude[SIZE_LATITUDE + UNO];
	char ptrLongitude[SIZE_LONGITUDE + UNO];

	memset(ptrCode, ZERO, SIZE_CODE  + UNO);
	memset(ptrNumber, ZERO, SIZE_NUMBER  + UNO);
	memset(ptrElevation, ZERO, SIZE_ELEVATION  + UNO);
	memset(ptrLatitude, ZERO, SIZE_LATITUDE  + UNO);
	memset(ptrLongitude, ZERO, SIZE_LONGITUDE  + UNO);

	strcpy(ptrCode, (char*)this->code.c_str());
	strcpy(ptrNumber, (char*)this->number.c_str());
	strcpy(ptrElevation, (char*)this->elevation.c_str());
	strcpy(ptrLatitude, (char*)this->latitude.c_str());
	strcpy(ptrLongitude, (char*)this->longitude.c_str());

	esquema = this->getSchema();
	
	RecordHandler::REG_SET(reg, esquema, "code, number, elevation, latitude, longitude", ptrCode, ptrNumber, ptrElevation, ptrLatitude, ptrLongitude);

}

bool Station::EsMayor(std::string atributte, Persistent *object)
{
	return (this->getValor(atributte) > object->getValor(atributte));
}

std::string Station::getCode()
{
	return code;
}

std::string Station::getKey(){
	return this->code;
}

void Station::setKey(std::string key){
	this->code = key;
}

std::string Station::getNumber()
{
	return this->number;
}

std::string Station::getElevation()
{
	return this->elevation;
}

std::string Station::getLatitude()
{
	return this->latitude;
}

std::string Station::getLongitude()
{
	return this->longitude;
}

void Station::setCode(std::string codigo)
{
	this->code = codigo;
}

void Station::setNumber(std::string numero)
{
	this->number = numero;
}

void Station::setLatitude(std::string latitud)
{
	this->latitude = latitud;
}

void Station::setElevation(std::string elevacion)
{
	this->elevation = elevacion;
}

void Station::setLongitude(std::string longitud)
{
	this->longitude = longitud;
}

const Station & Station::operator=(const Station& p)
{
	code = p.code;
	number = p.number;
	elevation = p.elevation;
	latitude = p.latitude;
	longitude = p.longitude;
	return (*this);

}

void Station::print()
{
	std::cout << "CODE: " << code << std::endl;
	std::cout << "NUMBER: " << number << std::endl;
	std::cout << "ELEVATION: " << elevation << std::endl;
	std::cout << "LATITUDE: " << latitude << std::endl;
	std::cout << "LONGITUDE: " << longitude << std::endl;
}

